﻿using IService.Blog;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace BlogAPI.Controllers
{
    /// <summary>
    /// 博主管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    //[AllowAnonymous]
    public class AboutController : ControllerBase
    {
        readonly ILogger<AboutController> logger;
        readonly IAboutService aboutService;
        public AboutController(ILogger<AboutController> _logger, IAboutService _aboutService)
        {
            logger = _logger;
            aboutService = _aboutService;
        }

        /// <summary>
        /// 查询所有博主（分页）
        /// </summary>
        /// <param name="offset">页数</param>
        /// <param name="limit">每页数</param>
        /// <returns></returns>
        [HttpPost]
        [Description("查询所有博主")]
        public async Task<MessageModel<PageInfoModel<BAbout>>> GetAllToPage(int offset=1, int limit=15)
        {
            try
            {
                return await aboutService.GetAllToPage(offset, limit);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<PageInfoModel<BAbout>>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
        }


        /// <summary>
        /// 查询所有博主
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("查询所有博主")]
        public async Task<MessageModel<List<BAbout>>> GetAll()
        {
            try
            {
                return await aboutService.GetAll();
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<List<BAbout>>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 按ID查询博主
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("按ID查询博主")]
        public async Task<MessageModel<BAbout>> GetById(int id)
        {
            try
            {
                return await aboutService.GetById(id);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<BAbout>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }


        /// <summary>
        /// 添加博主
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("添加博主")]
        public async Task<MessageModel<string>> Post([FromBody] BAbout parameter)
        {
            try
            {
                return await aboutService.Post(parameter);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 修改博主
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPut]
        [Description("修改博主")]
        public async Task<MessageModel<string>> Put([FromBody] BAbout parameter)
        {
            try
            {
                return await aboutService.Put(parameter);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 删除博主
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        [Description("删除博主")]
        public async Task<MessageModel<bool>> DelById(int id)
        {
            try
            {
                return await aboutService.DelById(id);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<bool>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }
    }
}
